/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Courses;
import entity.Coursetag;
import entity.Tag;
import entity.Tagrates;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.IllegalOrphanException;
import jpa.controllers.exceptions.NonexistentEntityException;
import jpa.controllers.exceptions.PreexistingEntityException;

/**
 *
 * @author atap
 */
public class CoursetagJpaController implements Serializable {

    public CoursetagJpaController(){        
    }
    
    public CoursetagJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
     private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Coursetag coursetag) throws PreexistingEntityException, Exception {
        if (coursetag.getTagratesList() == null) {
            coursetag.setTagratesList(new ArrayList<Tagrates>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Courses courseId = coursetag.getCourseId();
            if (courseId != null) {
                courseId = em.getReference(courseId.getClass(), courseId.getCourseId());
                coursetag.setCourseId(courseId);
            }
            Tag tagId = coursetag.getTagId();
            if (tagId != null) {
                tagId = em.getReference(tagId.getClass(), tagId.getId());
                coursetag.setTagId(tagId);
            }
            List<Tagrates> attachedTagratesList = new ArrayList<Tagrates>();
            for (Tagrates tagratesListTagratesToAttach : coursetag.getTagratesList()) {
                tagratesListTagratesToAttach = em.getReference(tagratesListTagratesToAttach.getClass(), tagratesListTagratesToAttach.getId());
                attachedTagratesList.add(tagratesListTagratesToAttach);
            }
            coursetag.setTagratesList(attachedTagratesList);
            em.persist(coursetag);
            if (courseId != null) {
                courseId.getCoursetagList().add(coursetag);
                courseId = em.merge(courseId);
            }
            if (tagId != null) {
                tagId.getCoursetagList().add(coursetag);
                tagId = em.merge(tagId);
            }
            for (Tagrates tagratesListTagrates : coursetag.getTagratesList()) {
                Coursetag oldCourseTagIdOfTagratesListTagrates = tagratesListTagrates.getCourseTagId();
                tagratesListTagrates.setCourseTagId(coursetag);
                tagratesListTagrates = em.merge(tagratesListTagrates);
                if (oldCourseTagIdOfTagratesListTagrates != null) {
                    oldCourseTagIdOfTagratesListTagrates.getTagratesList().remove(tagratesListTagrates);
                    oldCourseTagIdOfTagratesListTagrates = em.merge(oldCourseTagIdOfTagratesListTagrates);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCoursetag(coursetag.getId()) != null) {
                throw new PreexistingEntityException("Coursetag " + coursetag + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Coursetag coursetag) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Coursetag persistentCoursetag = em.find(Coursetag.class, coursetag.getId());
            Courses courseIdOld = persistentCoursetag.getCourseId();
            Courses courseIdNew = coursetag.getCourseId();
            Tag tagIdOld = persistentCoursetag.getTagId();
            Tag tagIdNew = coursetag.getTagId();
            List<Tagrates> tagratesListOld = persistentCoursetag.getTagratesList();
            List<Tagrates> tagratesListNew = coursetag.getTagratesList();
            List<String> illegalOrphanMessages = null;
            for (Tagrates tagratesListOldTagrates : tagratesListOld) {
                if (!tagratesListNew.contains(tagratesListOldTagrates)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tagrates " + tagratesListOldTagrates + " since its courseTagId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (courseIdNew != null) {
                courseIdNew = em.getReference(courseIdNew.getClass(), courseIdNew.getCourseId());
                coursetag.setCourseId(courseIdNew);
            }
            if (tagIdNew != null) {
                tagIdNew = em.getReference(tagIdNew.getClass(), tagIdNew.getId());
                coursetag.setTagId(tagIdNew);
            }
            List<Tagrates> attachedTagratesListNew = new ArrayList<Tagrates>();
            for (Tagrates tagratesListNewTagratesToAttach : tagratesListNew) {
                tagratesListNewTagratesToAttach = em.getReference(tagratesListNewTagratesToAttach.getClass(), tagratesListNewTagratesToAttach.getId());
                attachedTagratesListNew.add(tagratesListNewTagratesToAttach);
            }
            tagratesListNew = attachedTagratesListNew;
            coursetag.setTagratesList(tagratesListNew);
            coursetag = em.merge(coursetag);
            if (courseIdOld != null && !courseIdOld.equals(courseIdNew)) {
                courseIdOld.getCoursetagList().remove(coursetag);
                courseIdOld = em.merge(courseIdOld);
            }
            if (courseIdNew != null && !courseIdNew.equals(courseIdOld)) {
                courseIdNew.getCoursetagList().add(coursetag);
                courseIdNew = em.merge(courseIdNew);
            }
            if (tagIdOld != null && !tagIdOld.equals(tagIdNew)) {
                tagIdOld.getCoursetagList().remove(coursetag);
                tagIdOld = em.merge(tagIdOld);
            }
            if (tagIdNew != null && !tagIdNew.equals(tagIdOld)) {
                tagIdNew.getCoursetagList().add(coursetag);
                tagIdNew = em.merge(tagIdNew);
            }
            for (Tagrates tagratesListNewTagrates : tagratesListNew) {
                if (!tagratesListOld.contains(tagratesListNewTagrates)) {
                    Coursetag oldCourseTagIdOfTagratesListNewTagrates = tagratesListNewTagrates.getCourseTagId();
                    tagratesListNewTagrates.setCourseTagId(coursetag);
                    tagratesListNewTagrates = em.merge(tagratesListNewTagrates);
                    if (oldCourseTagIdOfTagratesListNewTagrates != null && !oldCourseTagIdOfTagratesListNewTagrates.equals(coursetag)) {
                        oldCourseTagIdOfTagratesListNewTagrates.getTagratesList().remove(tagratesListNewTagrates);
                        oldCourseTagIdOfTagratesListNewTagrates = em.merge(oldCourseTagIdOfTagratesListNewTagrates);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = coursetag.getId();
                if (findCoursetag(id) == null) {
                    throw new NonexistentEntityException("The coursetag with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Coursetag coursetag;
            try {
                coursetag = em.getReference(Coursetag.class, id);
                coursetag.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The coursetag with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Tagrates> tagratesListOrphanCheck = coursetag.getTagratesList();
            for (Tagrates tagratesListOrphanCheckTagrates : tagratesListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Coursetag (" + coursetag + ") cannot be destroyed since the Tagrates " + tagratesListOrphanCheckTagrates + " in its tagratesList field has a non-nullable courseTagId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Courses courseId = coursetag.getCourseId();
            if (courseId != null) {
                courseId.getCoursetagList().remove(coursetag);
                courseId = em.merge(courseId);
            }
            Tag tagId = coursetag.getTagId();
            if (tagId != null) {
                tagId.getCoursetagList().remove(coursetag);
                tagId = em.merge(tagId);
            }
            em.remove(coursetag);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Coursetag> findCoursetagEntities() {
        return findCoursetagEntities(true, -1, -1);
    }

    public List<Coursetag> findCoursetagEntities(int maxResults, int firstResult) {
        return findCoursetagEntities(false, maxResults, firstResult);
    }

    private List<Coursetag> findCoursetagEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Coursetag.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Coursetag findCoursetag(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Coursetag.class, id);
        } finally {
            em.close();
        }
    }

    public int getCoursetagCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Coursetag> rt = cq.from(Coursetag.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
